package com.weishenai.Utils;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JdbcUtils {
    private static String driver = null;
    private static String url = null;
    private static String username = null;
    private static String password = null;


    static {
        try {
            InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(inputStream);

            driver = properties.getProperty("jdbc.driver");
            url = properties.getProperty("jdbc.url");
            username = properties.getProperty("jdbc.username");
            password = properties.getProperty("jdbc.password");

            //加载驱动
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, username, password);
    }

    public static PreparedStatement getPreparedStatement(String sql) {
        try {
            return getConnection().prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void release(Connection connection) {
        release(connection, null, null);
    }

    public static void release(Statement statement) throws SQLException {
        release(getConnection(), statement, null);
    }

    public static void release(ResultSet resultSet) {
        release(null, null, resultSet);
    }

    public static void release(String sql) throws SQLException {
        release(getConnection(), getPreparedStatement(sql), null);
    }

    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        if (resultSet != null) {
            try {
                System.out.println("resultSet close");
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if (statement != null) {
            try {
                System.out.println("statement close");
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                System.out.println("connection close");
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


    //单例模式创建
    private static boolean flag = false;

    private static class jdbcUtilsHolder {
        private static final JdbcUtils INSTANCE = new JdbcUtils();
    }

    private JdbcUtils() {
        synchronized (JdbcUtils.class) {
            if (!flag) {
                flag = true;
            } else {
                throw new RuntimeException("单例被破坏");
            }
        }
    }

    public static final JdbcUtils getInstance() {
        return jdbcUtilsHolder.INSTANCE;
    }
}
